{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 其他有关pandas的话题\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import tushare as ts\n",
    "from numpy import nan as NA\n",
    "from pandas import DataFrame\n",
    "from pandas import Panel\n",
    "from pandas import Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 整数索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "-1",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-bcc879013620>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mser\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mSeries\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3.\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mser\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m  \u001b[1;31m# 这里与list不同，使用了整数索引，那么-1有可能出现在任何位置。\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\series.py\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m    599\u001b[0m         \u001b[0mkey\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_apply_if_callable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    600\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 601\u001b[0;31m             \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_value\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    602\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    603\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mis_scalar\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexes\\base.py\u001b[0m in \u001b[0;36mget_value\u001b[0;34m(self, series, key)\u001b[0m\n\u001b[1;32m   2475\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m   2476\u001b[0m             return self._engine.get_value(s, k,\n\u001b[0;32m-> 2477\u001b[0;31m                                           tz=getattr(series.dtype, 'tz', None))\n\u001b[0m\u001b[1;32m   2478\u001b[0m         \u001b[1;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m   2479\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m0\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minferred_type\u001b[0m \u001b[1;32min\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;34m'integer'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'boolean'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_value (pandas\\_libs\\index.c:4404)\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_value (pandas\\_libs\\index.c:4087)\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc (pandas\\_libs\\index.c:5126)\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;32mpandas\\_libs\\hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.Int64HashTable.get_item (pandas\\_libs\\hashtable.c:14031)\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;32mpandas\\_libs\\hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.Int64HashTable.get_item (pandas\\_libs\\hashtable.c:13975)\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;31mKeyError\u001b[0m: -1"
     ]
    }
   ],
   "source": [
    "ser = Series(np.arange(3.))\n",
    "ser[-1]  # 这里与list不同，使用了整数索引，那么-1有可能出现在任何位置。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0.0\n",
       "1    1.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser.iloc[:-1] # 但是用iloc的话就无视索引的值，-1的含义又变回位置了。iget_value函数已被取消。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser2 = Series(np.arange(3.), index=['a', 'b', 'c'])\n",
    "ser2[-1] # 非整数索引-1代表最后一个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0\n",
      "1    1\n",
      "Name: 2, dtype: int32\n",
      "0    2\n",
      "1    3\n",
      "Name: 0, dtype: int32\n",
      "2    1\n",
      "0    3\n",
      "Name: 1, dtype: int32\n"
     ]
    }
   ],
   "source": [
    "frame = DataFrame(np.arange(6).reshape(3, 2), index=[2, 0, 1])\n",
    "print(frame.iloc[0,]) # iloc指定行/列，irow函数已被取消。\n",
    "print(frame.iloc[1,:])\n",
    "print(frame.iloc[:-1,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 面板数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>close</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>price_change</th>\n",
       "      <th>p_change</th>\n",
       "      <th>ma5</th>\n",
       "      <th>ma10</th>\n",
       "      <th>ma20</th>\n",
       "      <th>v_ma5</th>\n",
       "      <th>v_ma10</th>\n",
       "      <th>v_ma20</th>\n",
       "      <th>turnover</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2017-06-30</th>\n",
       "      <td>5.16</td>\n",
       "      <td>5.25</td>\n",
       "      <td>5.25</td>\n",
       "      <td>5.14</td>\n",
       "      <td>2041933.62</td>\n",
       "      <td>0.07</td>\n",
       "      <td>1.35</td>\n",
       "      <td>5.140</td>\n",
       "      <td>5.103</td>\n",
       "      <td>5.117</td>\n",
       "      <td>2285876.87</td>\n",
       "      <td>2135653.75</td>\n",
       "      <td>1879762.83</td>\n",
       "      <td>0.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-29</th>\n",
       "      <td>5.09</td>\n",
       "      <td>5.19</td>\n",
       "      <td>5.18</td>\n",
       "      <td>5.08</td>\n",
       "      <td>1814934.50</td>\n",
       "      <td>0.11</td>\n",
       "      <td>2.17</td>\n",
       "      <td>5.118</td>\n",
       "      <td>5.082</td>\n",
       "      <td>5.119</td>\n",
       "      <td>2204673.43</td>\n",
       "      <td>2070232.88</td>\n",
       "      <td>1838141.19</td>\n",
       "      <td>0.07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-28</th>\n",
       "      <td>5.09</td>\n",
       "      <td>5.15</td>\n",
       "      <td>5.07</td>\n",
       "      <td>5.06</td>\n",
       "      <td>2450792.00</td>\n",
       "      <td>-0.02</td>\n",
       "      <td>-0.39</td>\n",
       "      <td>5.102</td>\n",
       "      <td>5.069</td>\n",
       "      <td>5.126</td>\n",
       "      <td>2465994.18</td>\n",
       "      <td>2086340.73</td>\n",
       "      <td>1804638.64</td>\n",
       "      <td>0.09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-27</th>\n",
       "      <td>5.10</td>\n",
       "      <td>5.12</td>\n",
       "      <td>5.09</td>\n",
       "      <td>5.06</td>\n",
       "      <td>2101331.75</td>\n",
       "      <td>-0.02</td>\n",
       "      <td>-0.39</td>\n",
       "      <td>5.098</td>\n",
       "      <td>5.077</td>\n",
       "      <td>5.135</td>\n",
       "      <td>2205762.78</td>\n",
       "      <td>1914710.43</td>\n",
       "      <td>1740952.72</td>\n",
       "      <td>0.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-26</th>\n",
       "      <td>5.13</td>\n",
       "      <td>5.24</td>\n",
       "      <td>5.11</td>\n",
       "      <td>5.07</td>\n",
       "      <td>3020392.50</td>\n",
       "      <td>-0.03</td>\n",
       "      <td>-0.58</td>\n",
       "      <td>5.088</td>\n",
       "      <td>5.083</td>\n",
       "      <td>5.142</td>\n",
       "      <td>2179352.73</td>\n",
       "      <td>1797820.74</td>\n",
       "      <td>1691910.98</td>\n",
       "      <td>0.11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            open  high  close   low      volume  price_change  p_change  \\\n",
       "date                                                                      \n",
       "2017-06-30  5.16  5.25   5.25  5.14  2041933.62          0.07      1.35   \n",
       "2017-06-29  5.09  5.19   5.18  5.08  1814934.50          0.11      2.17   \n",
       "2017-06-28  5.09  5.15   5.07  5.06  2450792.00         -0.02     -0.39   \n",
       "2017-06-27  5.10  5.12   5.09  5.06  2101331.75         -0.02     -0.39   \n",
       "2017-06-26  5.13  5.24   5.11  5.07  3020392.50         -0.03     -0.58   \n",
       "\n",
       "              ma5   ma10   ma20       v_ma5      v_ma10      v_ma20  turnover  \n",
       "date                                                                           \n",
       "2017-06-30  5.140  5.103  5.117  2285876.87  2135653.75  1879762.83      0.08  \n",
       "2017-06-29  5.118  5.082  5.119  2204673.43  2070232.88  1838141.19      0.07  \n",
       "2017-06-28  5.102  5.069  5.126  2465994.18  2086340.73  1804638.64      0.09  \n",
       "2017-06-27  5.098  5.077  5.135  2205762.78  1914710.43  1740952.72      0.08  \n",
       "2017-06-26  5.088  5.083  5.142  2179352.73  1797820.74  1691910.98      0.11  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gsyh = ts.get_hist_data('601398',start='2017-01-01',end='2017-06-30') # 工商银行\n",
    "jsyh = ts.get_hist_data('601939',start='2017-01-01',end='2017-06-30') # 建设银行\n",
    "jtyh = ts.get_hist_data('601328',start='2017-01-01',end='2017-06-30') # 交通银行\n",
    "gsyh.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<class 'pandas.core.panel.Panel'>\n",
       "Dimensions: 3 (items) x 119 (major_axis) x 14 (minor_axis)\n",
       "Items axis: gsyh to jtyh\n",
       "Major_axis axis: 2017-06-30 to 2017-01-03\n",
       "Minor_axis axis: open to turnover"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pdata = Panel({'gsyh': gsyh, 'jsyh': jsyh, 'jtyh': jtyh}) # 面板的每一层就是一个DataFrame\n",
    "# Item：Z轴2，gsyh/jsyh/jtyh\n",
    "# Major：X轴0，行，日期\n",
    "# Minor：Y轴1，列，open/high/...\n",
    "pdata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>close</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>price_change</th>\n",
       "      <th>p_change</th>\n",
       "      <th>ma5</th>\n",
       "      <th>ma10</th>\n",
       "      <th>ma20</th>\n",
       "      <th>v_ma5</th>\n",
       "      <th>v_ma10</th>\n",
       "      <th>v_ma20</th>\n",
       "      <th>turnover</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2017-06-30</th>\n",
       "      <td>5.16</td>\n",
       "      <td>5.25</td>\n",
       "      <td>5.25</td>\n",
       "      <td>5.14</td>\n",
       "      <td>2041933.62</td>\n",
       "      <td>0.07</td>\n",
       "      <td>1.35</td>\n",
       "      <td>5.140</td>\n",
       "      <td>5.103</td>\n",
       "      <td>5.117</td>\n",
       "      <td>2285876.87</td>\n",
       "      <td>2135653.75</td>\n",
       "      <td>1879762.83</td>\n",
       "      <td>0.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-29</th>\n",
       "      <td>5.09</td>\n",
       "      <td>5.19</td>\n",
       "      <td>5.18</td>\n",
       "      <td>5.08</td>\n",
       "      <td>1814934.50</td>\n",
       "      <td>0.11</td>\n",
       "      <td>2.17</td>\n",
       "      <td>5.118</td>\n",
       "      <td>5.082</td>\n",
       "      <td>5.119</td>\n",
       "      <td>2204673.43</td>\n",
       "      <td>2070232.88</td>\n",
       "      <td>1838141.19</td>\n",
       "      <td>0.07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-28</th>\n",
       "      <td>5.09</td>\n",
       "      <td>5.15</td>\n",
       "      <td>5.07</td>\n",
       "      <td>5.06</td>\n",
       "      <td>2450792.00</td>\n",
       "      <td>-0.02</td>\n",
       "      <td>-0.39</td>\n",
       "      <td>5.102</td>\n",
       "      <td>5.069</td>\n",
       "      <td>5.126</td>\n",
       "      <td>2465994.18</td>\n",
       "      <td>2086340.73</td>\n",
       "      <td>1804638.64</td>\n",
       "      <td>0.09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-27</th>\n",
       "      <td>5.10</td>\n",
       "      <td>5.12</td>\n",
       "      <td>5.09</td>\n",
       "      <td>5.06</td>\n",
       "      <td>2101331.75</td>\n",
       "      <td>-0.02</td>\n",
       "      <td>-0.39</td>\n",
       "      <td>5.098</td>\n",
       "      <td>5.077</td>\n",
       "      <td>5.135</td>\n",
       "      <td>2205762.78</td>\n",
       "      <td>1914710.43</td>\n",
       "      <td>1740952.72</td>\n",
       "      <td>0.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-26</th>\n",
       "      <td>5.13</td>\n",
       "      <td>5.24</td>\n",
       "      <td>5.11</td>\n",
       "      <td>5.07</td>\n",
       "      <td>3020392.50</td>\n",
       "      <td>-0.03</td>\n",
       "      <td>-0.58</td>\n",
       "      <td>5.088</td>\n",
       "      <td>5.083</td>\n",
       "      <td>5.142</td>\n",
       "      <td>2179352.73</td>\n",
       "      <td>1797820.74</td>\n",
       "      <td>1691910.98</td>\n",
       "      <td>0.11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            open  high  close   low      volume  price_change  p_change  \\\n",
       "date                                                                      \n",
       "2017-06-30  5.16  5.25   5.25  5.14  2041933.62          0.07      1.35   \n",
       "2017-06-29  5.09  5.19   5.18  5.08  1814934.50          0.11      2.17   \n",
       "2017-06-28  5.09  5.15   5.07  5.06  2450792.00         -0.02     -0.39   \n",
       "2017-06-27  5.10  5.12   5.09  5.06  2101331.75         -0.02     -0.39   \n",
       "2017-06-26  5.13  5.24   5.11  5.07  3020392.50         -0.03     -0.58   \n",
       "\n",
       "              ma5   ma10   ma20       v_ma5      v_ma10      v_ma20  turnover  \n",
       "date                                                                           \n",
       "2017-06-30  5.140  5.103  5.117  2285876.87  2135653.75  1879762.83      0.08  \n",
       "2017-06-29  5.118  5.082  5.119  2204673.43  2070232.88  1838141.19      0.07  \n",
       "2017-06-28  5.102  5.069  5.126  2465994.18  2086340.73  1804638.64      0.09  \n",
       "2017-06-27  5.098  5.077  5.135  2205762.78  1914710.43  1740952.72      0.08  \n",
       "2017-06-26  5.088  5.083  5.142  2179352.73  1797820.74  1691910.98      0.11  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pdata['gsyh'].head() # 选择gsyh层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\ipykernel\\__main__.py:16: DeprecationWarning: \n",
      "Panel is deprecated and will be removed in a future version.\n",
      "The recommended way to represent these types of 3-dimensional data are with a MultiIndex on a DataFrame, via the Panel.to_frame() method\n",
      "Alternatively, you can use the xarray package http://xarray.pydata.org/en/stable/.\n",
      "Pandas provides a `.to_xarray()` method to help automate this conversion.\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>gsyh</th>\n",
       "      <th>jsyh</th>\n",
       "      <th>jtyh</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2017-06-30</th>\n",
       "      <td>5.16</td>\n",
       "      <td>6.19</td>\n",
       "      <td>6.15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-29</th>\n",
       "      <td>5.09</td>\n",
       "      <td>6.40</td>\n",
       "      <td>6.14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-28</th>\n",
       "      <td>5.09</td>\n",
       "      <td>6.37</td>\n",
       "      <td>6.10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-27</th>\n",
       "      <td>5.10</td>\n",
       "      <td>6.31</td>\n",
       "      <td>6.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-26</th>\n",
       "      <td>5.13</td>\n",
       "      <td>6.34</td>\n",
       "      <td>6.16</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            gsyh  jsyh  jtyh\n",
       "date                        \n",
       "2017-06-30  5.16  6.19  6.15\n",
       "2017-06-29  5.09  6.40  6.14\n",
       "2017-06-28  5.09  6.37  6.10\n",
       "2017-06-27  5.10  6.31  6.11\n",
       "2017-06-26  5.13  6.34  6.16"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 靠，居然警告Panel以后不用。。。\n",
    "# 交换Items/Minors轴，交换后形式如下\n",
    "#\n",
    "#          /\n",
    "#         ...\n",
    "#       close\n",
    "#     open Z轴\n",
    "#   /\n",
    "# /\n",
    "# --- 银行名字 X轴\n",
    "# |\n",
    "# |\n",
    "# 日期 Y轴\n",
    "# |\n",
    "# |\n",
    "swaped_pdata = pdata.swapaxes('items', 'minor')\n",
    "swaped_pdata['open'].head() # 交换后的panel去open层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "date\n",
       "2017-06-30    5.16\n",
       "2017-06-29    5.09\n",
       "2017-06-28    5.09\n",
       "2017-06-27    5.10\n",
       "2017-06-26    5.13\n",
       "Name: open, dtype: float64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pdata.loc['gsyh', '2017-06-30':'2017-06-25', 'open'].head() # 轴顺序为Z-Y-X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>gsyh</th>\n",
       "      <th>jsyh</th>\n",
       "      <th>jtyh</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th>minor</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">2017-06-30</th>\n",
       "      <th>open</th>\n",
       "      <td>5.16</td>\n",
       "      <td>6.19</td>\n",
       "      <td>6.15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>high</th>\n",
       "      <td>5.25</td>\n",
       "      <td>6.19</td>\n",
       "      <td>6.17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>close</th>\n",
       "      <td>5.25</td>\n",
       "      <td>6.15</td>\n",
       "      <td>6.16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>low</th>\n",
       "      <td>5.14</td>\n",
       "      <td>6.10</td>\n",
       "      <td>6.13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>volume</th>\n",
       "      <td>2041933.62</td>\n",
       "      <td>932212.06</td>\n",
       "      <td>712699.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         gsyh       jsyh       jtyh\n",
       "date       minor                                   \n",
       "2017-06-30 open          5.16       6.19       6.15\n",
       "           high          5.25       6.19       6.17\n",
       "           close         5.25       6.15       6.16\n",
       "           low           5.14       6.10       6.13\n",
       "           volume  2041933.62  932212.06  712699.00"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame = pdata.loc[:, '2017-06-30':'2017-06-25', :].to_frame() # Y轴保持不变，X轴变成索引内层，Z轴变成列。\n",
    "frame.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>gsyh</th>\n",
       "      <th>jsyh</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th>minor</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">2017-06-30</th>\n",
       "      <th>open</th>\n",
       "      <td>5.16</td>\n",
       "      <td>6.19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>high</th>\n",
       "      <td>5.25</td>\n",
       "      <td>6.19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>close</th>\n",
       "      <td>5.25</td>\n",
       "      <td>6.15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>low</th>\n",
       "      <td>5.14</td>\n",
       "      <td>6.10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>volume</th>\n",
       "      <td>2041933.62</td>\n",
       "      <td>932212.06</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         gsyh       jsyh\n",
       "date       minor                        \n",
       "2017-06-30 open          5.16       6.19\n",
       "           high          5.25       6.19\n",
       "           close         5.25       6.15\n",
       "           low           5.14       6.10\n",
       "           volume  2041933.62  932212.06"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame = pdata.loc['gsyh':'jsyh', :, :].to_frame()\n",
    "frame.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
